package com.zretc.blog.controller;

import com.zretc.api.pojo.*;
import com.zretc.blog.service.BlogService;
import com.zretc.blog.service.CommentService;
import com.zretc.common.entity.R;
import com.zretc.common.entity.RespResult;
import com.zretc.security.entity.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisServer;
import org.springframework.http.MediaType;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/blog")
public class BlogController {
    @Autowired
    private BlogService blogService;
    @Autowired
    private CommentService commentService;



    private SysUser sysUser;
    @PostMapping("/add")
    public RespResult addBlog(@RequestBody Blog blog){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser user = (LoginUser) authentication.getPrincipal();
        return blogService.addBlog( blog,user.getUser().getUserId()) ;

    }
    @PostMapping("/addcomment")
    public RespResult addComment(@RequestBody Comment comment){

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser user = (LoginUser) authentication.getPrincipal();
        comment.setPostedBy(user.getUser().getUsername());
        return commentService.addComment( comment);
    }

    @PostMapping("/update")
    public RespResult updateBlog(@RequestBody Blog blog ){
        return blogService.updateBlog(blog);
    }
    @GetMapping("/getlist")
    public RespResult getBlogByBlogTitle( @RequestParam("target") String target,@RequestParam("pageNum") Integer pageNum,
                                          @RequestParam("pageSize") Integer pageSize){
        return R.success(blogService.queryBlogByBlogTitle(target,pageNum,pageSize));
    }
    @RequestMapping("/delete")
    public RespResult deleteBlog(@RequestBody Blog blog){
        return blogService.deleteBlog(blog);
    }
    @DeleteMapping("/deletecomment")
    public RespResult delete(@RequestBody Comment comment){
        return commentService.deletecomment(comment);
    }

    @GetMapping("/list")
    public RespResult list(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser user = (LoginUser) authentication.getPrincipal();

        return R.success(blogService.queryBlogByName(user.getUser().getUsername()));

    }


    @GetMapping("/blogDetail")
    public RespResult list(@RequestParam Integer blogId){

        return R.success(commentService.queryDetailByBlogId(blogId));
    }

    @GetMapping("/history")
    public RespResult list(@RequestBody User user){
        return R.success(commentService.queryCommentByUsername(user.getUsername()));
    }

    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public RespResult uploadFile(@RequestPart("file") MultipartFile file){
        return R.success(blogService.uploadpicture(file));
    }


    /*
     * 测试sns-point-service模块接口
     */
    @GetMapping("/testAddPoint")
    public RespResult testAddPoint(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser user = (LoginUser) authentication.getPrincipal();
        return blogService.testAddPoint(user.getUser().getUserId());
    }


    /*
     *  测试sns-file-service模块接口
     */
    @PostMapping(value = "testUpload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public RespResult testUpload(@RequestPart("file") MultipartFile file){
        System.out.println("testUpload=============BlogController======================================="); // test
        System.out.println(file.getOriginalFilename()); // test

        String[] s = blogService.testUpload(file);

        return R.success(s);
    }
}